#include "unp.h"

int main(int argc, char** argv)
{
  int sockfd,n;
  struct sockaddr_un servaddr,cliaddr;
  socklen_t len;
  char recvline[4096] = {'\0'};
  char sendline[4096] = {'\0'};
 
  if (argc < 2)
  {
    printf("usage: client <socket path>\n");
    exit(-1);
  }
  
  memset(&servaddr,0,sizeof(servaddr));
  memset(&cliaddr,0,sizeof(cliaddr));
  
  sockfd = socket(AF_LOCAL,SOCK_DGRAM,0);
  //unlink(argv[1]);
  cliaddr.sun_family = AF_LOCAL;
  strcpy(cliaddr.sun_path,tmpnam(NULL));
  printf("client bind path:%s\n",cliaddr.sun_path);
  servaddr.sun_family = AF_LOCAL;
  strcpy(servaddr.sun_path,argv[1]);
  bind(sockfd,(struct sockaddr*)(&cliaddr),sizeof(cliaddr));
  
  while(fgets(sendline,sizeof(sendline),stdin) != NULL)
  {
    sendto(sockfd,sendline,strlen(sendline),0,(const struct sockaddr*)&servaddr,sizeof(servaddr));
    n = recvfrom(sockfd,recvline,sizeof(recvline),0,NULL,NULL);
    recvline[n] = 0;
    fputs(recvline,stdout);
  } 
 
  printf("EOF...\n");
  shutdown(sockfd,SHUT_WR);
  close(sockfd);

  return 0;
}
